#include "StdAfx.h"
#include "StringUtil.h"

void StringUtil::Split(const tstring& toSplit, const tstring& delim, vector<tstring>& resultRef, bool eager){
	size_t index=0,next;

	resultRef.clear();
	do{
		next  = toSplit.find_first_of(delim,index);
		if ( next != string::npos) 
			resultRef.push_back(toSplit.substr(index,next-index));
		else
			resultRef.push_back(toSplit.substr(index));
		if ( eager )
			index = toSplit.find_first_not_of(delim,next);
		else 
			index = next+1;
	}while( next != string::npos);
}

tstring StringUtil::GetParentDirectory(tstring& fileName){
	{
		size_t slashIndex;
		slashIndex = fileName.find_last_of(_T("/"));
		if ( slashIndex != string::npos){
			return fileName.substr(0,slashIndex);
		}else{
			slashIndex = fileName.find_last_of(_T("\\"));
			if ( slashIndex != string::npos){
				return fileName.substr(0,slashIndex);
			}else{
				return _T("./");
			}
		}
	}
}

tstring StringUtil::GetLastDirectoryElement(tstring& filename){
	size_t slashIndex;
	slashIndex;
	// watch out for mixed case , i.e. a directory with both
	// forward and backward slashes.
	size_t slashIndex1 = filename.find_last_of(_T("/"));
	size_t slashIndex2 = filename.find_last_of(_T("\\"));
	if ( slashIndex1 != string::npos || slashIndex2 != string::npos){
		if (slashIndex1 != string::npos && slashIndex2 != string::npos )
		{
			size_t slashIndex = slashIndex1;
			if ( slashIndex2 > slashIndex )
				slashIndex = slashIndex2;
			return filename.substr(slashIndex+1);
		}else if ( slashIndex1 != string::npos ){
			return filename.substr(slashIndex1+1);
		}else{
			return filename.substr(slashIndex2+1);
		}
	}else{
		return filename;
	}
}

tstring& StringUtil::TrimWhiteSpace(tstring& toTrim)
{
	if(toTrim != _T(""))
	{
		if(toTrim.substr(0,1) == _T(" "))
			toTrim = toTrim.substr(1);
		if(toTrim.substr(toTrim.size() - 1,1) == _T(" "))
			toTrim = toTrim.substr(0, toTrim.size() - 1);
	}

	return toTrim;
}

void StringUtil::SplitQuotedStrings(const tstring &toSplit, vector<tstring>& resultRef)
{
	size_t index=0,first,second;
	resultRef.clear();

	do{
		first = toSplit.find_first_of(_T("\""),index);
		if ( first == tstring::npos )
			break;
		second = toSplit.find_first_of(_T("\""),first+1);
		if ( first != tstring::npos && second != tstring::npos)
			// do not include the quotes themselves
			resultRef.push_back(toSplit.substr(first+1, second-first-1));
		index = second+1;
	}while(second != tstring::npos);
}	
float StringUtil::ToFloat(tstring toConvert)
{
	tstringstream stream;
	float result;
	stream << toConvert;
	stream >> result;
	return result;
}

tstring StringUtil::ToString( int getal )
{
	tstringstream buffer;
	buffer << getal;
	return buffer.str();
}

tstring StringUtil::ToString( double getal )
{
	tstringstream buffer;
	buffer << getal;
	return buffer.str();
}

tstring StringUtil::ToString( float getal )
{
	tstringstream buffer;
	buffer << getal;
	return buffer.str();
}

tstring StringUtil::GetExtension( const tstring& file, tstring& ext )
{
	vector<tstring> parsehulp;
	Split(file, _T("."), parsehulp);
	ext = parsehulp.at(parsehulp.size() - 1);
	return ext;
}

int StringUtil::ToInt( tstring toConvert )
{
	return (int)ToFloat(toConvert);
}